<?php
namespace apiadmin\modules\v1\controllers\trade;

use apiadmin\modules\v1\controllers\CoreController;
use common\models\GroupFinalist;
use common\models\StaticInstance;

class  StaticInstanceController extends CoreController
{
    public function actionList()
    {
        $where  = $this->formartWhere();
        $member_id = $this->request('member_id');
        if ($member_id){
            $where['where']=['member_id'=>$member_id];
        }

        $params = array(
            'field'	=> ['*'],
            'order' => 'create_time desc',
            'page'	=> $this->request('page','1'),
            'limit' => $this->request('page_size',10),
        );

        $list =StaticInstance::dataList($where,$params,['home_list']);

        $pages = StaticInstance::$pages;

        $this->out('我的合约列表',$list,array('pages'=>$pages));
    }

    //组装条件
    public function formartWhere()
    {
        $where = [];
        $whereAnd = [];
        $searchKeys = json_decode($this->request('search'),1);

        if(!$searchKeys) return array('where'=>$where,'whereAnd'=>$whereAnd);
        foreach($searchKeys as $k=>$val)
        {
            if(!$val) continue;
            if($k=='date')
            {
                if(!$val['0'] || !$val['1']) continue;
                $whereAnd[] = ['between', 'create_time', date('Y-m-d',strtotime($val[0])),date('Y-m-d',strtotime($val[1]))];
            }elseif ($k=='member_name') {
                $whereAnd[] = ['like',$k,$val];
            }else
            {
                $where[$k] = $val;
            }
        }

        return array('where'=>$where,'and'=>$whereAnd);
    }
}